<template>
  <view class="container">
    <!-- 页面标题 -->
    <view class="header">
      <view class="back-btn" @click="goBack">
        <uni-icons type="left" size="18" color="#333"></uni-icons>
      </view>
      <text class="title">比赛报名</text>
      <view class="placeholder"></view>
    </view>
    
    <!-- 报名信息 -->
    <view class="section-card">
      <view class="card-header">
        <text class="section-title">比赛信息</text>
      </view>
      <view class="comp-info">
        <text class="comp-name">{{competitionName}}</text>
        <view class="comp-meta">
          <text class="comp-time">比赛时间：{{competitionTime || '待定'}}</text>
          <text class="comp-location">比赛地点：{{competitionLocation || '待定'}}</text>
        </view>
      </view>
    </view>
    
    <!-- 参赛信息 -->
    <view class="section-card">
      <view class="card-header">
        <text class="section-title">参赛信息</text>
      </view>
      <view class="form-item">
        <text class="form-label">参赛者姓名</text>
        <input class="form-input" type="text" v-model="formData.name" placeholder="请输入参赛者姓名" />
      </view>
      <view class="form-item">
        <text class="form-label">学校</text>
        <input class="form-input" type="text" v-model="formData.school" placeholder="请输入学校名称" />
      </view>
      <view class="form-item">
        <text class="form-label">年级</text>
        <picker class="form-picker" mode="selector" :range="gradeOptions" @change="gradeChange">
          <view class="picker-view">
            {{formData.grade || '请选择年级'}}
          </view>
        </picker>
      </view>
      <view class="form-item">
        <text class="form-label">联系电话</text>
        <input class="form-input" type="number" v-model="formData.phone" placeholder="请输入联系电话" />
      </view>
      <view class="form-item">
        <text class="form-label">联系邮箱</text>
        <input class="form-input" type="text" v-model="formData.email" placeholder="请输入联系邮箱" />
      </view>
    </view>
    
    <!-- 报名须知 -->
    <view class="section-card">
      <view class="card-header">
        <text class="section-title">报名须知</text>
      </view>
      <view class="notice-content">
        <text class="notice-text">1. 请确保填写信息准确无误，特别是联系方式；</text>
        <text class="notice-text">2. 报名成功后，将在3个工作日内收到确认邮件；</text>
        <text class="notice-text">3. 如需修改报名信息，请联系客服；</text>
        <text class="notice-text">4. 报名截止日期: {{registrationDeadline || '待定'}}。</text>
      </view>
      <view class="agreement">
        <checkbox :checked="agreed" @click="toggleAgreement" color="#1890ff" style="transform:scale(0.7)" />
        <text class="agreement-text">我已阅读并同意《比赛规则》和《隐私政策》</text>
      </view>
    </view>
    
    <!-- 提交按钮 -->
    <view class="action-bar">
      <view class="fee-info" v-if="registrationFee">
        <text>报名费: </text>
        <text class="fee">¥{{registrationFee}}</text>
      </view>
      <button class="action-btn" :disabled="!canSubmit" @click="submitRegistration">
        提交报名
      </button>
    </view>
  </view>
</template>

<script>
export default {
  data() {
    return {
      competitionId: '',
      competitionName: '',
      competitionTime: '',
      competitionLocation: '',
      registrationDeadline: '',
      registrationFee: '',
      gradeOptions: ['小学四年级', '小学五年级', '小学六年级', '初中一年级', '初中二年级', '初中三年级', '高中一年级', '高中二年级', '高中三年级'],
      formData: {
        name: '',
        school: '',
        grade: '',
        phone: '',
        email: ''
      },
      agreed: false
    };
  },
  computed: {
    canSubmit() {
      return this.agreed && 
        this.formData.name && 
        this.formData.school && 
        this.formData.grade && 
        this.formData.phone;
    }
  },
  onLoad(options) {
    if (options.id) {
      this.competitionId = options.id;
      // 假设这里会根据ID请求比赛详情
      this.loadCompetitionData(options.id);
    }
  },
  methods: {
    loadCompetitionData(id) {
      // 这里实际应用会从服务器获取数据
      // 这里使用假数据示例
      this.competitionName = decodeURIComponent(id);
      this.competitionTime = '2023年12月10日 09:00-12:00';
      this.competitionLocation = '各省指定考点';
      this.registrationDeadline = '2023年12月1日';
      this.registrationFee = '120';
      
      // 预填充学生信息
      this.formData.name = '王小明'; // 从全局状态获取当前学生信息
      this.formData.school = '实验中学';
      this.formData.grade = '初中二年级';
    },
    goBack() {
      uni.navigateBack();
    },
    gradeChange(e) {
      this.formData.grade = this.gradeOptions[e.detail.value];
    },
    toggleAgreement() {
      this.agreed = !this.agreed;
    },
    submitRegistration() {
      if (!this.canSubmit) {
        return;
      }
      
      uni.showLoading({
        title: '提交中...'
      });
      
      // 模拟提交请求
      setTimeout(() => {
        uni.hideLoading();
        uni.showToast({
          title: '报名成功',
          icon: 'success',
          duration: 2000
        });
        
        // 跳转回详情页
        setTimeout(() => {
          uni.navigateBack();
        }, 2000);
      }, 1500);
    }
  }
};
</script>

<style>
.container {
  padding: 30rpx;
  padding-bottom: 120rpx;
  background-color: #f5f7fa;
  min-height: 100vh;
}

.header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 30rpx;
}

.back-btn {
  width: 60rpx;
  height: 60rpx;
  display: flex;
  align-items: center;
  justify-content: center;
}

.title {
  font-size: 34rpx;
  font-weight: bold;
  color: #333;
}

.placeholder {
  width: 60rpx;
}

.section-card {
  background-color: #fff;
  border-radius: 16rpx;
  padding: 30rpx;
  margin-bottom: 30rpx;
  box-shadow: 0 2rpx 10rpx rgba(0, 0, 0, 0.05);
}

.card-header {
  margin-bottom: 20rpx;
}

.section-title {
  font-size: 30rpx;
  font-weight: bold;
  color: #333;
}

.comp-name {
  font-size: 32rpx;
  font-weight: bold;
  color: #333;
  margin-bottom: 16rpx;
}

.comp-meta {
  display: flex;
  flex-direction: column;
}

.comp-time, .comp-location {
  font-size: 26rpx;
  color: #666;
  margin-bottom: 8rpx;
}

.form-item {
  margin-bottom: 24rpx;
}

.form-label {
  font-size: 28rpx;
  color: #333;
  margin-bottom: 12rpx;
  display: block;
}

.form-input, .form-picker {
  width: 100%;
  height: 80rpx;
  background-color: #f5f7fa;
  border-radius: 8rpx;
  padding: 0 20rpx;
  font-size: 28rpx;
  color: #333;
}

.picker-view {
  height: 80rpx;
  line-height: 80rpx;
  color: #333;
}

.notice-content {
  display: flex;
  flex-direction: column;
}

.notice-text {
  font-size: 26rpx;
  color: #666;
  margin-bottom: 12rpx;
  line-height: 1.5;
}

.agreement {
  display: flex;
  align-items: center;
  margin-top: 16rpx;
}

.agreement-text {
  font-size: 26rpx;
  color: #666;
}

.action-bar {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  height: 100rpx;
  background-color: #fff;
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0 30rpx;
  box-shadow: 0 -2rpx 10rpx rgba(0, 0, 0, 0.05);
}

.fee-info {
  font-size: 28rpx;
  color: #666;
}

.fee {
  font-size: 32rpx;
  color: #f04864;
  font-weight: bold;
}

.action-btn {
  width: 240rpx;
  height: 70rpx;
  line-height: 70rpx;
  background-color: #1890ff;
  color: #fff;
  border-radius: 35rpx;
  font-size: 28rpx;
}

.action-btn[disabled] {
  background-color: #cccccc;
  color: #ffffff;
  opacity: 0.6;
}
</style> 